package layui_mybatis.controller;

import layui_mybatis.entity.Province;
import layui_mybatis.service.ProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by zhangzheng on 2020/8/19.
 */
@Controller
@RequestMapping("/provinces")
public class ProvinceController {

    @Autowired
    private ProvinceService provinceService;


    @RequestMapping("/show")
    public String show(){
        return "redirect:/province.html";
    }

    @RequestMapping("/treeload")
    @ResponseBody
    public Object getProvince(){
        List<Province> provinces=provinceService.getProvince();
        List<HashMap<String,Object>> result=new ArrayList<>();
        System.out.println("======================="+fun(provinces,result));
        return fun(provinces,result);
    }

    private Object fun(List<Province> province, List<HashMap<String,Object>> result){
        for (Province p: province) {
            HashMap<String,Object> map=new HashMap<>();
            map.put("id",p.getId());
            map.put("title",p.getPname());
            map.put("spread",true);
            List<HashMap<String,Object>> result1=new ArrayList<>();
            List<Province> childrens=p.getChildren();
            map.put("children",fun(childrens,result1));
            result.add(map);
        }
     return result;
    }

    @RequestMapping("/test")
    private void test(){
        provinceService.Test();
    }
}
